#!/bin/sh

#
# Test that metadata can be retrieved a .fw file
# when only some of that file is streamed in the framed way
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"
create_15M_file

EXPECTED_OUTPUT=$WORK/expected_output
ACTUAL_OUTPUT=$WORK/actual_output

cat >$CONFIG <<EOF
meta-product = "product name"
meta-description = "product description"
meta-version = "some version"
meta-platform = "a platform"
meta-architecture = "an architecture"
meta-author = "someone"

file-resource bigfile {
	host-path = "${TESTFILE_15M}"
}
EOF

cat >$EXPECTED_OUTPUT <<EOF
meta-product="product name"
meta-description="product description"
meta-version="some version"
meta-author="someone"
meta-platform="a platform"
meta-architecture="an architecture"
meta-creation-date="2018-05-05T18:10:16Z"
meta-uuid="62a285ff-25b5-5c14-ea5f-0f5286b0e198"
EOF

cat >$EXPECTED_OUTPUT.type << EOF
OK
EOF

SOURCE_DATE_EPOCH=1525543816 $FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Only keep the first 512 bytes of the whole 15MB .fw file!
dd if=$FWFILE of=$FWFILE.truncated bs=1 count=512 2>/dev/null

# Pipe the framed contents (in super small chunks) on the .fw file to fwup
cat $FWFILE.truncated | $FRAMING_HELPER -n 50 -e \
	| $FWUP_APPLY_NO_CHECK --metadata --framing > $ACTUAL_OUTPUT

# Verify and removing framing
cat $ACTUAL_OUTPUT | $FRAMING_HELPER -d > $ACTUAL_OUTPUT.noframing

# Check the textual component
cat $ACTUAL_OUTPUT.noframing | (dd bs=1 skip=4 2>/dev/null) > $ACTUAL_OUTPUT.trimmed
diff -w $EXPECTED_OUTPUT $ACTUAL_OUTPUT.trimmed

# Check the type
cat $ACTUAL_OUTPUT.noframing | (dd bs=1 count=2 2>/dev/null) > $ACTUAL_OUTPUT.type
diff -w $EXPECTED_OUTPUT.type $ACTUAL_OUTPUT.type
